package com.study.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.study.app.domain.AppUserAddress;
import com.study.app.domain.bo.AppUserAddressBo;
import com.study.app.domain.vo.AppUserAddressVo;
import com.study.app.mapper.AppUserAddressMapper;
import com.study.app.service.IAppUserAddressService;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@RequiredArgsConstructor
@Service
public class AppUserAddressServiceImpl implements IAppUserAddressService {

    private final AppUserAddressMapper baseMapper;


    @Override
    public List<AppUserAddressVo> getAddressByUser(AppUserAddressBo bo) {

        LambdaQueryWrapper<AppUserAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppUserAddress::getUserCode, bo.getUserCode())
                .orderByAsc(AppUserAddress::getIsDefault)
                .orderByDesc(AppUserAddress::getCreateTime);

        List<AppUserAddressVo> appUserAddressVos = baseMapper.selectVoList(wrapper);
        for (AppUserAddressVo appUserAddressVo : appUserAddressVos) {

            //TODO ID转name 解密


        }


        return appUserAddressVos;
    }

    @Override
    public Boolean addAddress(AppUserAddressBo bo) {

        AppUserAddress appUserAddress = new AppUserAddress();
        BeanUtils.copyProperties(bo, appUserAddress);
        appUserAddress.setCreateTime(new Date());
        int insert = baseMapper.insert(appUserAddress);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateAddress(AppUserAddressBo bo) {

        AppUserAddress appUserAddress = new AppUserAddress();
        BeanUtils.copyProperties(bo, appUserAddress);
        appUserAddress.setUpdateTime(new Date());
        int update = baseMapper.updateById(appUserAddress);
        if (update > 0) {
            return true;
        }

        return null;
    }
}
